#include <stdio.h>
#include <stdlib.h>

#include "log.h"

#define CONF_NAME "yaupspe.cfg"

/* Note: for now, the config file only contains the ISO path */

void conf_save(s8 *key, s8 *value)
{
    FILE *f = NULL;
    f = fopen(CONF_NAME, "w");
    if (!f) {
        _log(ERR, CORE, "Can't save to config file!");
        return;
    }

    _log(INF, CORE, "Saving %s = %s in config", key, value);

    fprintf(f, "%s", value);

    fclose(f);
}

s8 *conf_load(s8 *key)
{
    FILE *f = NULL;
    s32 len, c;
    s8 *value;

    f = fopen(CONF_NAME, "r");
    if (!f) {
        _log(ERR, CORE, "Can't load config file!");
        return NULL;
    }

    _log(INF, CORE, "Loading key %s in config", key);

    for (len = 0;; len++)
    {
        c = fgetc(f);
        if (c == '\n' || c == EOF)
            break;
    }

    value = malloc(len + 1);

    fseek(f, 0, SEEK_SET);
    if (fscanf(f, "%s", value) == 0)
        _log(ERR, CORE, "Empty value?");

    fclose(f);

    return value;
}

void conf_free(s8 *value)
{
    free(value);
}

